<?php

class Banners_model extends Model {

    public function listaBanners()
    {
        try {
            
            $banners = Doctrine::getTable('NwsBanner')->findAll();
            return $banners;
        } catch (Doctrine_Exception $e) {
            print $e->getMessage();
        }   
    }
    	
    public function listaPosicoes()
    {
        try {
            $posicoes = Doctrine::getTable('NwsBannerLoc')->findAll();
            return $posicoes;
        } catch (Doctrine_Exception $e) {
            print $e->getMessage();
        }   
    }
    
    public function excluirBanner($banCod)
    {
        $banners = Doctrine::getTable('NwsBanner')->find($banCod);
        if($banners) {
            $banners->delete();
            header('location:'. BASE_URL_ADM . 'banners');
        } else {
            echo "errado";
        }
    }
    
    public function excluirPosicao($codPos)
    {
        $banners = Doctrine::getTable('NwsBannerLoc')->find($codPos);
        if($banners) {
            $banners->delete();
            header('location:'. BASE_URL_ADM . 'banners/posicoes/');
        } else {
            echo "errado";
        }
    }
    
    public function cadastroPosicoes()
    {
        $bannersloc = new NwsBannerLoc;
        $bannersloc->setdesPos($_POST['desPos']);
        $bannersloc->inserirBannerPosicao(); 
        header('location:'. BASE_URL_ADM . 'banners/posicoes/');
    }
    
    public function editaPosicoes($codPos,$desPos)
    {
        $bannersloc = Doctrine_Query::create()
        ->update('NwsBannerLoc')
        ->set('desPos', "'".$desPos."'")
        ->where('codPos = '.$codPos );
        $bannersloc->execute();
        header('location:'. BASE_URL_ADM . 'banners');
    }
    
    public function editarBannerPosicao($codPos)
    {
        $ban_pos = Doctrine::getTable('NwsBannerLoc')->find($codPos);
        return $ban_pos;
    }
    
    public function listaMarcas()
    {
        $marcas = Doctrine::getTable('NwsProdutoMarca')->findAll();
        return $marcas;
    }
    
    public function insertBanner()
    {
        $banner = new NwsBanner;
        $banner->setbanTip($_POST['banTip']);
        $banner->setmarCod($_POST['marCod']);
        $banner->setbanLoc($_POST['banLoc']);
        $banner->setbanTar($_POST['banTar']);
        $banner->setbanTit($_POST['banTit']);
        $banner->setbanUrl($_POST['banUrl']);               
        $banner->setbanMail($_POST['banMail']);
        $banner->setbanSta($_POST['banSta']);
        $banner->setbanDatIns(date("Y-m-d h:i:s"));
        $uploader = new Upload($_FILES['banArq']);
        if ($uploader->uploaded) {
        	$name_pattern = $_FILES['banArq']['name'];
        	$uploader->file_new_name_body = $name_pattern;
        	$uploader->file_overwrite = true;
        	$uploader->file_auto_rename = false;
        	$uploader->Process(BASE_UPL.'/banners/');
        	if ($uploader->processed) {
        	   $banner->setbanArq($uploader->file_dst_name);
        	   $banner->inserirBanner(); 
        	} else {
        	   echo "Upload com Erro";
        	}
    	}
        $uploader->Clean();
        header('location:'. BASE_URL_ADM . 'banners/');
    }
    
    public function editarBanner($banCod)
    {
        $banner = Doctrine::getTable('NwsBanner')->find($banCod);
        return $banner;
    }
    
    public function editaBannerdb($post)
     {
         $clientes = Doctrine_Query::create()
         ->update('NwsBanner')
         ->set('banTip', "'".$post['banTip']."'")
         ->set('marCod', "'".$post['marCod']."'")
         ->set('banLoc', "'".$post['banLoc']."'")
         ->set('banTar', "'".$post['banTar']."'")
         ->set('banTit', "'".$post['banTit']."'")
         ->set('banUrl', "'".$post['banUrl']."'")
         ->set('banMail', "'".$post['banMail']."'")
         ->set('banSta', "'".$post['banSta']."'")
         ->where('banCod = '.$post['banCod'] );
         $clientes->execute();
         header('location:'. BASE_URL_ADM . 'banners/');
     }
     
     public function editaBannerImagem($post,$file,$banArq)
     {
        unlink(BASE_UPL.'banners/'.$banArq);
        $uploader = new Upload($file['banArq']);
        if ($uploader->uploaded) {
        	$name_pattern = $file['banArq']['name'];
        	$uploader->file_new_name_body = $name_pattern;
        	$uploader->file_overwrite = true;
        	$uploader->file_auto_rename = false;
        	$uploader->Process(BASE_UPL.'/banners/');
        	if ($uploader->processed) {
        	   $banners = Doctrine_Query::create()
               ->update('NwsBanner')
               ->set('banArq', "'".$uploader->file_dst_name."'")
               ->where('banCod = '.$post['banCod'] );
               $banners->execute();
        	} else {
        	   echo "Upload com Erro";
        	}
    	}
        $uploader->Clean();
        header('location:'. BASE_URL_ADM . 'banners/');
     }
     
     public function trocaStatus($banCod,$banSta)
     {
        $status = Doctrine_Query::create()
        ->update('NwsBanner')
        ->set('banSta', "'".$banSta."'")
        ->where('banCod = '.$banCod);
        $status->execute();
        header('location:'. BASE_URL_ADM . 'banners/');
     }
}

?>
